<?php

declare(strict_types=1);

namespace App\Model;

use Hyperf\DbConnection\Model\Model;

/**
 *  OcpcOppoLog
 */
class OcpcOppoLog extends Model
{

//    public const CHANNELS = [404,504];
    public const CHANNELS = [1002];

    protected $table = 'ocpc_oppo_log';

    static function addLog($params = [])
    {
        $m = new self();
        $m->oaid = $params['oaid'] ?? '';
        $m->imei = $params['imei'] ?? '';
        $m->pay_amount = $params['payAmount'] ?? 0;
        $m->user_id = $params['user_id'] ?? 0;
        $m->sign = $params['sign'];
        $m->res = $params['res'];
        $m->pkg = $params['pkg'];
        $m->type = $params['dataType'];
        $m->is_register = $params['is_register'] ?? 0;
        return $m->saveOrFail() ? 1 : 0;
    }

    //当天注册人数
    public static function getRegisterUser($startDate,$endDate,$package){
        $num = self::select(['user_id'])->where([
            ['created_at','>=',$startDate],
            ['created_at','<=',$endDate],
            ['pkg','=',$package],
            ['type','=',2],
        ])->groupBy(['user_id'])->get()->toArray();
        return count($num);
    }

    //当天充值人数
    public static function getRechargeUser($startDate,$endDate,$package,$isReg=false){

        $where = [
            ['created_at','>=',$startDate],
            ['created_at','<=',$endDate],
            ['pkg','=',$package],
            ['type','=',7],
        ];

        if($isReg){
            $where[] = ['is_register','=',1];
        }

        $num = self::select(['user_id'])->where($where)->groupBy(['user_id'])->get()->toArray();
        return count($num);
    }

    //当天充值金额 分
    public static function getRechargeMoney($startDate,$endDate,$package,$isReg=false){

        $where = [
            ['created_at','>=',$startDate],
            ['created_at','<=',$endDate],
            ['pkg','=',$package],
            ['type','=',7],
        ];

        if($isReg){
            $where[] = ['is_register','=',1];
        }

        $num = self::where($where)->sum('pay_amount');
        return $num;
    }

    //当天充值次数
    public static function getRechargeNum($startDate,$endDate,$package,$isReg=false){

        $where = [
            ['created_at','>=',$startDate],
            ['created_at','<=',$endDate],
            ['pkg','=',$package],
        ];

        if($isReg){
            $where[] = ['is_register','=',1];
        }
        $num = self::where($where)->whereIn('type',[3,7])->count();
        return $num;
    }




}
